//------------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "main.h"
#include "process.h"
#include <mmsystem.h>
#include "canlib\\canlib.h"
#include "varinit.h"
#include "About.h"
//------------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

TMainForm *MainForm;


//------------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
        : TForm(Owner)
{

}
//------------------------------------------------------------------------------
void WINAPI TaskCycle(UINT uID,UINT MSG,DWORD DWUSER,DWORD DW1,DWORD DW2)
{
     Cycle10ms();
}
//------------------------------------------------------------------------------
void TMainForm::Start_Timer(unsigned int ms_time_val)
{
     TIMECAPS TC;
     UINT TIMERES=0;
     UINT TIMEID;
     unsigned int Interval;

     Interval=(int)ms_time_val;

     if(timeGetDevCaps(&TC,sizeof(TIMECAPS))!=TIMERR_NOERROR)
     {
         exit(0);
     }
     timeBeginPeriod(TIMERES);

     TIMEID=timeSetEvent(Interval,TIMERES,&(LPTIMECALLBACK)TaskCycle,0,TIME_PERIODIC);
     g_TIMEID=TIMEID;
}
//------------------------------------------------------------------------------
void TMainForm::End_timer(void)
{
     timeKillEvent(g_TIMEID);
     timeEndPeriod(0);
}
//------------------------------------------------------------------------------
void Cycle10ms(void)
{
     switch (counterCycle1--)
     {
     case 9:
          Msg1Send();
          break;
     case 8:
          break;
     case 7:
          Msg2Send();
          break;
     case 6:
          break;
     case 5:
          Msg3Send();
          break;
     case 4:
          break;
     case 3:
          break;
     case 2:
          Msg4Send();
          break;
     case 1:
          break;
     case 0:
          Cycle100ms();
          counterCycle1 = 9;
          break;
     default:
          break;
     }
}
//------------------------------------------------------------------------------
void Cycle100ms(void)
{
     switch (counterCycle2--)
     {
     case 9:
          break;
     case 8:
          break;
     case 7:
          break;
     case 6:
          break;
     case 5:
          break;
     case 4:
          break;
     case 3:
          break;
     case 2:
          break;
     case 1:
          break;
     case 0:
          counterCycle2 = 9;
          Cycle1000ms();
          break;
     default:
          break;
     }
}
//------------------------------------------------------------------------------
void Cycle1000ms(void)
{
     switch (counterCycle3--)
     {
     case 9:
          break;
     case 8:
          break;
     case 7:
          break;
     case 6:
          break;
     case 5:
          break;
     case 4:
          break;
     case 3:
          break;
     case 2:
          break;
     case 1:
          break;
     case 0:
          counterCycle3 = 9;
          break;
     default:
          break;
     }
}

//------------------------------------------------------------------------------
void __fastcall TMainForm::FormCreate(TObject *Sender)
{
     int i, chanCount;

     char name[64];
     //------------------------------------------
     canInitializeLibrary();
     if (canGetNumberOfChannels(&chanCount) < 0)
     {
        return;
     }

     ChannelCombo->Items->Clear();
     for (i=0; i<chanCount; i++)
     {
          canGetChannelData(i, canCHANNELDATA_CHANNEL_NAME, name, sizeof(name));
          ChannelCombo->Items->Add((AnsiString)name);
     }
     Handle1 = Handle2 = canINVALID_HANDLE;
     //------------------------------------------
     ChannelCombo->ItemIndex = 0;
     ChannelComboChange(ChannelCombo);
     ComboBox1Change(ComboBox1);
     //------------------------------------------


//   canSetBusParams(Handle1, BAUD_50K, 0, 0, 0, 0, 0);
     SelectedBitTime.speed = 50000;
     SelectedBitTime.tseg1 = 6 ;
     SelectedBitTime.tseg2 = 2;
     SelectedBitTime.sjw = 1;
     SelectedBitTime.sp  = 75;
/*     Label13->Caption = Format("canSetBusParams(hnd, %d, %d, %d, %d, %d, %d);",
          ARRAYOFCONST((
          (int)SelectedBitTime.speed,
          (int)SelectedBitTime.tseg1-1,
          (int)SelectedBitTime.tseg2,
          (int)SelectedBitTime.sjw,1,0))
          );  */

     SJWEdit->Text= SelectedBitTime.sjw ;
     SPTEdit->Text=SelectedBitTime.sp;
     //------------------------------------------
     BitBtn_msg1_UpdateClick(BitBtn_msg1_Update);
     BitBtn_msg2_UpdateClick(BitBtn_msg2_Update);
     BitBtn_msg3_UpdateClick(BitBtn_msg3_Update);
     BitBtn_msg4_UpdateClick(BitBtn_msg4_Update);
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::ChannelComboChange(TObject *Sender)
{
     canClose(Handle1);
     Handle1 = canOpenChannel(ChannelCombo->ItemIndex, canWANT_VIRTUAL);
     if (Handle1 < 0)
     {
          ShowMessage("Can't open channel.");
     }
     BitBtn5Click(BitBtn5);
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::BitBtn4Click(TObject *Sender)
{ 
     canBusOff(Handle1);
     canSetNotify(Handle1, Handle, canNOTIFY_RX);
     canBusOn(Handle1);
     //-------------------------------------------
     TimerRx->Enabled = true;
     //-------------------------------------------
     BusStateTimer->Enabled = true;
     //-------------------------------------------
     BusStRadioGroup->ItemIndex = 0;

     TxCounterOld = 0;
     RxCounterOld = 0;
     TxCounter = 0;
     RxCounter = 0;
     TxTotalLabel->Caption = TxCounter;
     RxTotalLabel->Caption = RxCounter;
     TxPerLabel->Caption   = TxCounter - TxCounterOld;
     RxPerLabel->Caption   = RxCounter - RxCounterOld;
     //-------------------------------------------
     Start_Timer(1);
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::BitBtn5Click(TObject *Sender)
{
     TimerRx->Enabled = false;
     BusStateTimer->Enabled = false;
     //----------------------------
     canBusOff(Handle1);
     BusStRadioGroup->ItemIndex =2;
     End_timer();
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::BusStateTimerTimer(TObject *Sender)
{
     DWORD  can_status;
     TxTotalLabel->Caption = TxCounter;
     RxTotalLabel->Caption = RxCounter;
     TxPerLabel->Caption   = TxCounter - TxCounterOld;
     RxPerLabel->Caption   = RxCounter - RxCounterOld;
     TxCounterOld = TxCounter;
     RxCounterOld = RxCounter;
}

//---------------------------------------------------------------------------

void __fastcall TMainForm::About1Click(TObject *Sender)
{
    AboutForm->ShowModal();
}
//--------------------------------------------------------------------------- 

void __fastcall TMainForm::TimerRxTimer(TObject *Sender)
{
     bool ScanData = true;
     //--------------------------------------------------------
     while(ScanData)
     {
          stat=canRead(handle, &id, RxData, &dlc, &flags, &timestamp);
          if ((stat==canOK) && (dlc>0))
          {
              RxCounter++;

              //if((int)id == DIAG_RESP_ID)
              {
                  ReceiveSts = true;
                  /* display the rx message */
                  MainForm->MemoText->Lines->Add("Rx: " + IntToHex((int)id,3) + "  "
                                       + IntToHex((int)dlc,2) + "  "
                                       + IntToHex(RxData[0],2) + "  "
                                       + IntToHex(RxData[1],2) + "  "
                                       + IntToHex(RxData[2],2) + "  "
                                       + IntToHex(RxData[3],2) + "  "
                                       + IntToHex(RxData[4],2) + "  "
                                       + IntToHex(RxData[5],2) + "  "
                                       + IntToHex(RxData[6],2) + "  "
                                       + IntToHex(RxData[7],2) + "    "
                                       + Format("%.3f  T", ARRAYOFCONST(((float)timestamp/1000))));
               } 
          }
          else
          {
               ScanData=false;
          }
     }
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::BitBtn1Click(TObject *Sender)
{
//     canBusOff(Handle1);
//     canSetBusParams(Handle1, SelectedBitTime.speed, SelectedBitTime.tseg1-1, SelectedBitTime.tseg2, SelectedBitTime.sjw, 1, 0);
}
//---------------------------------------------------------------------------


void __fastcall TMainForm::BitBtn7Click(TObject *Sender)
{
     MemoText->Clear();
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::Exit1Click(TObject *Sender)
{
     exit(EXIT_SUCCESS);       
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::BitBtn_msg1_UpdateClick(TObject *Sender)
{
     AnsiString Tempx = "";

     Tempx = "0x"+Edit_msg1_ID->Text;
     Can_Msg1.id  = Tempx.ToInt();
     Tempx =  "0x"+Edit_msg1_DLC->Text;
     Can_Msg1.dlc = (unsigned char)Tempx.ToInt();

     Tempx =  "0x"+Edit_msg1_data1->Text;
     Can_Msg1.data[0] = (unsigned char)Tempx.ToInt();

     Tempx =  "0x"+Edit_msg1_data2->Text;
     Can_Msg1.data[1] = (unsigned char)Tempx.ToInt();

     Tempx =  "0x"+Edit_msg1_data3->Text;
     Can_Msg1.data[2] = (unsigned char)Tempx.ToInt();

     Tempx =  "0x"+Edit_msg1_data4->Text;
     Can_Msg1.data[3] = (unsigned char)Tempx.ToInt();

     Tempx =  "0x"+Edit_msg1_data5->Text;
     Can_Msg1.data[4] = (unsigned char)Tempx.ToInt();

     Tempx =  "0x"+Edit_msg1_data6->Text;
     Can_Msg1.data[5] = (unsigned char)Tempx.ToInt();

     Tempx =  "0x"+Edit_msg1_data7->Text;
     Can_Msg1.data[6] = (unsigned char)Tempx.ToInt();

     Tempx =  "0x"+Edit_msg1_data8->Text;
     Can_Msg1.data[7] = (unsigned char)Tempx.ToInt();

     Tempx =  Edit_Cycle1->Text;
     MsgCountValue1 =  ((unsigned char)Tempx.ToInt())/10;
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::BitBtn_msg2_UpdateClick(TObject *Sender)
{
     AnsiString Tempx = "";

     Tempx =  "0x"+Edit_msg2_ID->Text;
     Can_Msg2.id  = Tempx.ToInt();
     Tempx =  "0x"+Edit_msg2_DLC->Text;
     Can_Msg2.dlc = (unsigned char)Tempx.ToInt();

     Tempx =  "0x"+Edit_msg2_data1->Text;
     Can_Msg2.data[0] = (unsigned char)Tempx.ToInt();

     Tempx =  "0x"+Edit_msg2_data2->Text;
     Can_Msg2.data[1] = (unsigned char)Tempx.ToInt();

     Tempx =  "0x"+Edit_msg2_data3->Text;
     Can_Msg2.data[2] = (unsigned char)Tempx.ToInt();

     Tempx =  "0x"+Edit_msg2_data4->Text;
     Can_Msg2.data[3] = (unsigned char)Tempx.ToInt();

     Tempx =  "0x"+Edit_msg2_data5->Text;
     Can_Msg2.data[4] = (unsigned char)Tempx.ToInt();

     Tempx =  "0x"+Edit_msg2_data6->Text;
     Can_Msg2.data[5] = (unsigned char)Tempx.ToInt();

     Tempx =  "0x"+Edit_msg2_data7->Text;
     Can_Msg2.data[6] = (unsigned char)Tempx.ToInt();

     Tempx =  "0x"+Edit_msg2_data8->Text;
     Can_Msg2.data[7] = (unsigned char)Tempx.ToInt();

     Tempx = Edit_Cycle2->Text;
     MsgCountValue2 =  ((unsigned char)Tempx.ToInt())/10;
}

/*****************************************************************************/
/**
* Function Name: void Msg1Send(void)
* Description:
*
* Param:   none
* Return:  none
* Author:
******************************************************************************/
void Msg1Send(void)
{
    if(MainForm->CheckBox_msg1->Checked && (MsgCount1++ >= MsgCountValue1))
    {
       MsgCount1 = 0;

       if(canWrite(Handle1, Can_Msg1.id, Can_Msg1.data, Can_Msg1.dlc, canMSG_STD)==canOK)
       {
          TxCounter++;
          unsigned long timestamp;
          /* read time stamp */
          timestamp = canReadTimer(Handle1);

          /* display tx message */
          MainForm->MemoText->Lines->Add("Tx: " + IntToHex((int)(Can_Msg1.id),3) + "  "
                                       + IntToHex((int)(Can_Msg1.dlc),2) + "  "
                                       + IntToHex(Can_Msg1.data[0],2) + "  "
                                       + IntToHex(Can_Msg1.data[1],2) + "  "
                                       + IntToHex(Can_Msg1.data[2],2) + "  "
                                       + IntToHex(Can_Msg1.data[3],2) + "  "
                                       + IntToHex(Can_Msg1.data[4],2) + "  "
                                       + IntToHex(Can_Msg1.data[5],2) + "  "
                                       + IntToHex(Can_Msg1.data[6],2) + "  "
                                       + IntToHex(Can_Msg1.data[7],2) + "    "
                                       + Format("%.3f  T", ARRAYOFCONST(((float)timestamp/1000))));

       }
    }
}
/*****************************************************************************/
/**
* Function Name: void Msg2Send(void)
* Description:
*
* Param:   none
* Return:  none
* Author:
******************************************************************************/
void Msg2Send(void)
{
    if(MainForm->CheckBox_msg2->Checked && (MsgCount2++ >= MsgCountValue2))
    {
       MsgCount2 = 0;

       if(canWrite(Handle1, Can_Msg2.id, Can_Msg2.data, Can_Msg2.dlc, canMSG_STD)==canOK)
       {
          TxCounter++;
          unsigned long timestamp;
          // read time stamp
          timestamp = canReadTimer(Handle1);

          // display tx message 
          MainForm->MemoText->Lines->Add("Tx: " + IntToHex((int)(Can_Msg2.id),3) + "  "
                                       + IntToHex((int)(Can_Msg2.dlc),2) + "  "
                                       + IntToHex(Can_Msg2.data[0],2) + "  "
                                       + IntToHex(Can_Msg2.data[1],2) + "  "
                                       + IntToHex(Can_Msg2.data[2],2) + "  "
                                       + IntToHex(Can_Msg2.data[3],2) + "  "
                                       + IntToHex(Can_Msg2.data[4],2) + "  "
                                       + IntToHex(Can_Msg2.data[5],2) + "  "
                                       + IntToHex(Can_Msg2.data[6],2) + "  "
                                       + IntToHex(Can_Msg2.data[7],2) + "    "
                                       + Format("%.3f  T", ARRAYOFCONST(((float)timestamp/1000))));

       }
    }
}
/*****************************************************************************/
/**
* Function Name: void Msg3Send(void)
* Description:
*
* Param:   none
* Return:  none
* Author:
******************************************************************************/
void Msg3Send(void)
{
    if(MainForm->CheckBox_msg3->Checked && (MsgCount3++ >= MsgCountValue3))
    {
       MsgCount3 = 0;

       if(canWrite(Handle1, Can_Msg3.id, Can_Msg3.data, Can_Msg3.dlc, canMSG_STD)==canOK)
       {
          TxCounter++;
          unsigned long timestamp;
          /* read time stamp */
          timestamp = canReadTimer(Handle1);

          /* display tx message */
          MainForm->MemoText->Lines->Add("Tx: " + IntToHex((int)(Can_Msg3.id),3) + "  "
                                       + IntToHex((int)(Can_Msg3.dlc),2) + "  "
                                       + IntToHex(Can_Msg3.data[0],2) + "  "
                                       + IntToHex(Can_Msg3.data[1],2) + "  "
                                       + IntToHex(Can_Msg3.data[2],2) + "  "
                                       + IntToHex(Can_Msg3.data[3],2) + "  "
                                       + IntToHex(Can_Msg3.data[4],2) + "  "
                                       + IntToHex(Can_Msg3.data[5],2) + "  "
                                       + IntToHex(Can_Msg3.data[6],2) + "  "
                                       + IntToHex(Can_Msg3.data[7],2) + "    "
                                       + Format("%.3f  T", ARRAYOFCONST(((float)timestamp/1000))));

       }
    }
}
/*****************************************************************************/
/**
* Function Name: void Msg4Send(void)
* Description:
*
* Param:   none
* Return:  none
* Author:
******************************************************************************/
void Msg4Send(void)
{
   if(MainForm->CheckBox_msg4->Checked && (MsgCount4++ >= MsgCountValue4))
    {
       MsgCount4 = 0;

       if(canWrite(Handle1, Can_Msg4.id, Can_Msg4.data, Can_Msg4.dlc, canMSG_STD)==canOK)
       {
          TxCounter++;
          unsigned long timestamp;
          // read time stamp
          timestamp = canReadTimer(Handle1);

          //display tx message
          MainForm->MemoText->Lines->Add("Tx: " + IntToHex((int)(Can_Msg4.id),3) + "  "
                                       + IntToHex((int)(Can_Msg4.dlc),2) + "  "
                                       + IntToHex(Can_Msg4.data[0],2) + "  "
                                       + IntToHex(Can_Msg4.data[1],2) + "  "
                                       + IntToHex(Can_Msg4.data[2],2) + "  "
                                       + IntToHex(Can_Msg4.data[3],2) + "  "
                                       + IntToHex(Can_Msg4.data[4],2) + "  "
                                       + IntToHex(Can_Msg4.data[5],2) + "  "
                                       + IntToHex(Can_Msg4.data[6],2) + "  "
                                       + IntToHex(Can_Msg4.data[7],2) + "    "
                                       + Format("%.3f  T", ARRAYOFCONST(((float)timestamp/1000))));

       }
    }  
}

//------------------------------------------------------------------------------
void __fastcall TMainForm::BitBtn_msg3_UpdateClick(TObject *Sender)
{
     AnsiString Tempx = "";

     Tempx =  "0x"+Edit_msg3_ID->Text;
     Can_Msg3.id  = Tempx.ToInt();
     Tempx =  "0x"+Edit_msg3_DLC->Text;
     Can_Msg3.dlc = (unsigned char)Tempx.ToInt();

     Tempx =  "0x"+Edit_msg3_data1->Text;
     Can_Msg3.data[0] = (unsigned char)Tempx.ToInt();

     Tempx =  "0x"+Edit_msg3_data2->Text;
     Can_Msg3.data[1] = (unsigned char)Tempx.ToInt();

     Tempx =  "0x"+Edit_msg3_data3->Text;
     Can_Msg3.data[2] = (unsigned char)Tempx.ToInt();

     Tempx =  "0x"+Edit_msg3_data4->Text;
     Can_Msg3.data[3] = (unsigned char)Tempx.ToInt();

     Tempx =  "0x"+Edit_msg3_data5->Text;
     Can_Msg3.data[4] = (unsigned char)Tempx.ToInt();

     Tempx =  "0x"+Edit_msg3_data6->Text;
     Can_Msg3.data[5] = (unsigned char)Tempx.ToInt();

     Tempx =  "0x"+Edit_msg3_data7->Text;
     Can_Msg3.data[6] = (unsigned char)Tempx.ToInt();

     Tempx =  "0x"+Edit_msg3_data8->Text;
     Can_Msg3.data[7] = (unsigned char)Tempx.ToInt();

     Tempx =  Edit_Cycle3->Text;
     MsgCountValue3 =  ((unsigned char)Tempx.ToInt())/10;
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::BitBtn_msg4_UpdateClick(TObject *Sender)
{
     AnsiString Tempx = "";

     Tempx =  "0x"+Edit_msg4_ID->Text;
     Can_Msg4.id  = Tempx.ToInt();
     Tempx =  "0x"+Edit_msg4_DLC->Text;
     Can_Msg4.dlc = (unsigned char)Tempx.ToInt();

     Tempx =  "0x"+Edit_msg4_data1->Text;
     Can_Msg4.data[0] = (unsigned char)Tempx.ToInt();

     Tempx =  "0x"+Edit_msg4_data2->Text;
     Can_Msg4.data[1] = (unsigned char)Tempx.ToInt();

     Tempx =  "0x"+Edit_msg4_data3->Text;
     Can_Msg4.data[2] = (unsigned char)Tempx.ToInt();

     Tempx =  "0x"+Edit_msg4_data4->Text;
     Can_Msg4.data[3] = (unsigned char)Tempx.ToInt();

     Tempx =  "0x"+Edit_msg4_data5->Text;
     Can_Msg4.data[4] = (unsigned char)Tempx.ToInt();

     Tempx =  "0x"+Edit_msg4_data6->Text;
     Can_Msg4.data[5] = (unsigned char)Tempx.ToInt();

     Tempx =  "0x"+Edit_msg4_data7->Text;
     Can_Msg4.data[6] = (unsigned char)Tempx.ToInt();

     Tempx =  "0x"+Edit_msg4_data8->Text;
     Can_Msg4.data[7] = (unsigned char)Tempx.ToInt();

     Tempx =  Edit_Cycle4->Text;
     MsgCountValue4 =  ((unsigned char)Tempx.ToInt())/10;
}
//---------------------------------------------------------------------------


void __fastcall TMainForm::BitBtn3Click(TObject *Sender)
{
     TxCounterOld = 0;
     RxCounterOld = 0;
     TxCounter = 0;
     RxCounter = 0;
     TxTotalLabel->Caption = TxCounter;
     RxTotalLabel->Caption = RxCounter;
     TxPerLabel->Caption   = TxCounter - TxCounterOld;
     RxPerLabel->Caption   = RxCounter - RxCounterOld;
}
//---------------------------------------------------------------------------



void __fastcall TMainForm::ComboBox1Change(TObject *Sender)//zaza+
{
     long freq;

     switch (ComboBox1->ItemIndex)
     {
         case 0: freq = BAUD_500K; break;
         case 1: freq = BAUD_250K; break;
         case 2: freq = BAUD_125K; break;
         case 3: freq = BAUD_100K; break;
         case 4: freq = BAUD_83K; break;
         case 5: freq = BAUD_50K; break;

     }
     BitBtn5Click(BitBtn5);
     canSetBusParams(Handle1, freq, 0, 0, 0, 0, 0);

     BitBtn4Click(BitBtn4);

}
//---------------------------------------------------------------------------



